package com.leapcloud.crm.core.generate;

import freemarker.template.Configuration;
import freemarker.template.Template;
import lombok.extern.slf4j.Slf4j;
import org.jooq.DSLContext;

import java.io.*;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.*;

@Slf4j
public abstract class GenerateBase {

  protected static final DSLContext dslContext = new JooqFactory().getDSLContext();

  public static String srcFolder = "generatorCode";

  public static String javaScriptDir = "/core/src/main/resources/generate";

  public static String webScriptDir = "/core/src/main/resources/generateWeb";

  /**
   * 生成的文件存放的包，公共基础包
   */
  public static String packageBase = "com.leapcloud.crm";

  public static String[] entityCommonColums = {"created", "created_by", "last_modified", "last_modified_by", "is_deleted"};


  /**
   * 获取所有数据类型
   *
   * @param tableName
   * @return
   */
  public Set<String> getJataTypeList(String tableName) {
    Map<String, String> map = getJavaType(tableName);
    Set<String> keys = map.keySet();
    Set<String> typeSet = new HashSet<>();
    for (String key : keys) {
      String type = map.get(key);
      if ("byte[]".equals(type) || type.startsWith("java.lang.")) {
        continue;
      }
      typeSet.add(map.get(key));
    }
    return typeSet;
  }


  /**
   * 生成dao
   *
   * @param className
   * @param classNameSmall
   */
  public void dao(String tableName, String tableComment, List<TableColumnDTO> columnList) {
    String className = changeClassName(tableName, true);
    String classNameSmall = changeClassName(tableName, false);
    Map<String, Object> paraMap = new HashMap<>();
    String packages = packageBase + ".core.dao";
    paraMap.put("package", packages);
    paraMap.put("packageBase", packageBase);
    paraMap.put("className", className);
    paraMap.put("classNameSmall", classNameSmall);

    paraMap.put("columnList", columnList);
    paraMap.put("tableName", tableName);
    paraMap.put("tableComment", tableComment);
    paraMap.put("date", new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date()));

    String filePath = handleFilePath("core", className, packages, "Dao");
    createFileByTemplete("dao.ftl", paraMap, filePath, true);
  }


  /**
   * 生成DTO
   *
   * @param className
   * @param classNameSmall
   * @param dataSource
   * @param tableName
   */
  public void dto(String module, String tableName, String tableComment, List<TableColumnDTO> columnList) {
    String className = changeClassName(tableName, true);
    String classNameSmall = changeClassName(tableName, false);
    Map<String, Object> paraMap = new HashMap<>();
    String packages = packageBase + "." + module + ".dto";// + className.toLowerCase();
    paraMap.put("package", packages);
    paraMap.put("packageBase", packageBase);
    paraMap.put("className", className);
    paraMap.put("classNameSmall", classNameSmall);

    paraMap.put("columnList", columnList);
    paraMap.put("tableComment", tableComment);
    paraMap.put("dataTypes", getJataTypeList(tableName));
    paraMap.put("date", new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date()));

    String filePath = handleFilePath(module, className, packages, "DTO");
    createFileByTemplete("dto.ftl", paraMap, filePath, true);
  }

  /**
   * 生成VO
   *
   * @param className
   * @param classNameSmall
   * @param dataSource
   * @param tableName
   */
  public void vo(String module, String tableName, String tableComment, List<TableColumnDTO> columnList) {
    String className = changeClassName(tableName, true);
    String classNameSmall = changeClassName(tableName, false);
    Map<String, Object> paraMap = new HashMap<>();
    String packages = packageBase + "." + module + ".vo";// + "." + className.toLowerCase();
    paraMap.put("package", packages);
    paraMap.put("packageBase", packageBase);
    paraMap.put("className", className);
    paraMap.put("classNameSmall", classNameSmall);

    paraMap.put("columnList", columnList);
    paraMap.put("tableComment", tableComment);
    paraMap.put("dataTypes", getJataTypeList(tableName));
    paraMap.put("date", new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date()));

    String filePath = handleFilePath(module, className, packages, "VO");
    createFileByTemplete("vo.ftl", paraMap, filePath, true);
  }

  /**
   * 生成SearchVO
   *
   * @param className
   * @param classNameSmall
   * @param dataSource
   * @param tableName
   */
  public void searchVO(String module, String tableName, String tableComment, List<TableColumnDTO> columnList) {
    String className = changeClassName(tableName, true);
    String classNameSmall = changeClassName(tableName, false);
    Map<String, Object> paraMap = new HashMap<>();
    String packages = packageBase + "." + module + ".vo";// + "." + className.toLowerCase();
    paraMap.put("package", packages);
    paraMap.put("packageBase", packageBase);
    paraMap.put("className", className);
    paraMap.put("classNameSmall", classNameSmall);

    paraMap.put("columnList", columnList);
    paraMap.put("tableComment", tableComment);
    paraMap.put("dataTypes", getJataTypeList(tableName));
    paraMap.put("date", new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date()));

    String filePath = handleFilePath(module, className, packages, "SearchVO");
    createFileByTemplete("searchVO.ftl", paraMap, filePath, true);
  }


  /**
   * 生成Service
   *
   * @param className
   * @param classNameSmall
   */
  public void service(String module, String tableName, String tableComment) {
    String className = changeClassName(tableName, true);
    String classNameSmall = changeClassName(tableName, false);
    Map<String, Object> paraMap = new HashMap<>();
    String packages = packageBase + "." + module + ".service";// + "." + className.toLowerCase();
    paraMap.put("package", packages);
    paraMap.put("packageBase", packageBase);
    paraMap.put("module", module);
    paraMap.put("className", className);
    paraMap.put("classNameSmall", classNameSmall);
    paraMap.put("tableComment", tableComment);
    paraMap.put("date", new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date()));

    String filePath = handleFilePath(module, className, packages, "Service");
    createFileByTemplete("service.ftl", paraMap, filePath, true);
  }

  /**
   * 生成ServiceImpl
   *
   * @param className
   * @param classNameSmall
   */
  public void serviceImpl(String module, String tableName, String tableComment) {
    String className = changeClassName(tableName, true);
    String classNameSmall = changeClassName(tableName, false);
    Map<String, Object> paraMap = new HashMap<>();
    String packages = packageBase + "." + module + ".service.impl";// + "." + className.toLowerCase();
    paraMap.put("package", packages);
    paraMap.put("packageBase", packageBase);
    paraMap.put("module", module);
    paraMap.put("className", className);
    paraMap.put("classNameSmall", classNameSmall);

    paraMap.put("tableName", tableName);
    paraMap.put("tableComment", tableComment);
    paraMap.put("date", new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date()));

    String filePath = handleFilePath(module, className, packages, "ServiceImpl");
    ;
    createFileByTemplete("serviceImpl.ftl", paraMap, filePath, true);
  }


  /**
   * 生成biz 业务接口类
   *
   * @param module
   * @param tableName
   * @param tableComment
   * @param columnList
   */
  public void biz(String module, String tableName, String tableComment) {
    String className = changeClassName(tableName, true);
    String classNameSmall = changeClassName(tableName, false);
    Map<String, Object> paraMap = new HashMap<>();
    String packages = packageBase + "." + module + ".biz";// + "." + className.toLowerCase();
    paraMap.put("package", packages);
    paraMap.put("packageBase", packageBase);
    paraMap.put("module", module);
    paraMap.put("className", className);
    paraMap.put("classNameSmall", classNameSmall);

    paraMap.put("tableComment", tableComment);

    paraMap.put("date", new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date()));

    String filePath = handleFilePath(module, className, packages, "Biz");
    createFileByTemplete("biz.ftl", paraMap, filePath, true);
  }

  /**
   * 生成bizImpl 业务实现类
   *
   * @param module
   * @param tableName
   * @param tableComment
   * @param columnList
   */
  public void bizImpl(String module, String tableName, String tableComment, List<TableColumnDTO> columnList) {
    String className = changeClassName(tableName, true);
    String classNameSmall = changeClassName(tableName, false);
    Map<String, Object> paraMap = new HashMap<>();
    String packages = packageBase + "." + module + ".biz.impl";// + "." + className.toLowerCase();
    paraMap.put("package", packages);
    paraMap.put("packageBase", packageBase);
    paraMap.put("module", module);
    paraMap.put("className", className);
    paraMap.put("classNameSmall", classNameSmall);
    paraMap.put("columnList", columnList);
    paraMap.put("tableName", tableName);
    paraMap.put("tableComment", tableComment);
    paraMap.put("date", new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date()));

    String filePath = handleFilePath(module, className, packages, "BizImpl");
    createFileByTemplete("bizImpl.ftl", paraMap, filePath, true);
  }


  /**
   * 生成Controller
   *
   * @param className
   * @param classNameSmall
   */
  public void controller(String module, String tableName, String tableComment) {
    String className = changeClassName(tableName, true);
    String classNameSmall = changeClassName(tableName, false);
    Map<String, Object> paraMap = new HashMap<>();
    String packages = packageBase + "." + module + ".controller";
    paraMap.put("package", packages);
    paraMap.put("packageBase", packageBase);
    paraMap.put("module", module);
    paraMap.put("className", className);
    paraMap.put("classNameSmall", classNameSmall);

    paraMap.put("path", tableName.replace("_", "/"));


    paraMap.put("tableComment", tableComment);
    paraMap.put("date", new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date()));
    String filePath = handleFilePath(module, className, packages, "Controller");
    createFileByTemplete("controller.ftl", paraMap, filePath, true);
  }

  public String handleFilePath(String module, String className, String packages, String attach) {
    return handleFilePath(srcFolder, module, className, packages, "", attach, ".java");
  }

  public String handleWebFilePath(String module, String moduleName, String packages, String prefix, String attach) {
    StringBuilder filePath = new StringBuilder();
    filePath.append(System.getProperty("user.dir")).append("/")
        .append(srcFolder+"/web").append("/")
        .append(module).append("-module/")
        .append(packages.replace(".", "/")).append("/")
        .append(prefix).append(moduleName).append(attach).append(".js");
    return filePath.toString();
  }

  public String handleFilePath(String src, String module, String className, String packages, String prefix, String attach, String suffix) {
    StringBuilder filePath = new StringBuilder();
    filePath.append(System.getProperty("user.dir")).append("/")
        .append(src).append("/")
        .append(module).append("/")
        .append(packages.replace(".", "/")).append("/")
        .append(prefix).append(className).append(attach).append(suffix);
    return filePath.toString();
  }

  ///////////////////////////////////   前端模板   /////////////////////////////////////////


  public void webAction(String module, String prefix, String tableName, String tableComment) {
    Map<String, Object> paraMap = new HashMap<>();
    String className = changeClassName(tableName, true);
    String modelName = changeClassName(tableName.replace(prefix + "_", ""), true);

    String modelNameSmall = changeClassName(tableName.replace(prefix + "_", ""), false);

    String packages = "actions";
    paraMap.put("className", className);
    paraMap.put("module", module);
    paraMap.put("modelName", modelName);
    paraMap.put("modelNameSmall", modelNameSmall);

    paraMap.put("path", tableName.replace("_", "/"));

    paraMap.put("tableName", tableName);
    paraMap.put("tableComment", tableComment);
    paraMap.put("date", new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date()));
    String filePath = handleWebFilePath(module, className, packages, "", "Action");
    createWebFileByTemplete("Action.ftl", paraMap, filePath, true);
  }

  public void webReducer(String module, String prefix, String tableName, String tableComment) {
    Map<String, Object> paraMap = new HashMap<>();
    String className = changeClassName(tableName, true);
    String modelName = changeClassName(tableName.replace(prefix + "_", ""), true);

    String modelNameSmall = changeClassName(tableName.replace(prefix + "_", ""), false);

    String packages = "reducers";
    paraMap.put("className", className);
    paraMap.put("module", module);
    paraMap.put("modelName", modelName);
    paraMap.put("modelNameSmall", modelNameSmall);

    paraMap.put("tableName", tableName);
    paraMap.put("tableComment", tableComment);
    paraMap.put("date", new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date()));
    String filePath = handleWebFilePath(module, className, packages, "", "Reducer");
    createWebFileByTemplete("Reducer.ftl", paraMap, filePath, true);
  }


  public void webCompsIndex(String module, String prefix, String tableName, String tableComment) {
    Map<String, Object> paraMap = new HashMap<>();

    String className = changeClassName(tableName, true);
    String moduleName = changeClassName(tableName.replace(prefix + "_", ""), true);

    String moduleNameSmall = changeClassName(tableName.replace(prefix + "_", ""), false);

    String packages = "components." + moduleNameSmall;
    paraMap.put("className", className);
    paraMap.put("module", module);
    paraMap.put("moduleName", moduleName);
    paraMap.put("moduleNameSmall", moduleNameSmall);

    paraMap.put("tableComment", tableComment);
    paraMap.put("date", new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date()));
    String filePath = handleWebFilePath(module, "", packages, "", "index");
    createWebFileByTemplete("module/index.ftl", paraMap, filePath, true);
  }


  public void webCompsAdd(String module, String prefix, String tableName, String tableComment) {
    Map<String, Object> paraMap = new HashMap<>();
    String className = changeClassName(tableName, true);
    String moduleName = changeClassName(tableName.replace(prefix + "_", ""), true);

    String moduleNameSmall = changeClassName(tableName.replace(prefix + "_", ""), false);

    String packages = "components." + moduleNameSmall + ".add";
    paraMap.put("className", className);
    paraMap.put("module", module);
    paraMap.put("moduleName", moduleName);
    paraMap.put("moduleNameSmall", moduleNameSmall);


    paraMap.put("tableComment", tableComment);
    paraMap.put("date", new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date()));
    String filePath = handleWebFilePath(module, moduleName, packages, "Add", "Modal");
    createWebFileByTemplete("module/add/AddModal.ftl", paraMap, filePath, true);
  }

  public void webCompsEdit(String module, String prefix, String tableName, String tableComment) {
    Map<String, Object> paraMap = new HashMap<>();
    String className = changeClassName(tableName, true);
    String moduleName = changeClassName(tableName.replace(prefix + "_", ""), true);
    String moduleNameSmall = changeClassName(tableName.replace(prefix + "_", ""), false);
    String packages ="components." + moduleNameSmall + ".edit";

    paraMap.put("className", className);
    paraMap.put("module", module);
    paraMap.put("moduleName", moduleName);
    paraMap.put("moduleNameSmall", moduleNameSmall);


    paraMap.put("tableComment", tableComment);
    paraMap.put("date", new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date()));
    String filePath = handleWebFilePath(module, moduleName, packages, "Edit", "Modal");
    createWebFileByTemplete("module/edit/EditModal.ftl", paraMap, filePath, true);
  }

  public void webCompsForm(String module, String prefix, String tableName, String tableComment, List<TableColumnDTO> columnList) {
    Map<String, Object> paraMap = new HashMap<>();
    String className = changeClassName(tableName, true);
    String moduleName = changeClassName(tableName.replace(prefix + "_", ""), true);

    String moduleNameSmall = changeClassName(tableName.replace(prefix + "_", ""), false);

    String packages = "components." + moduleNameSmall + ".form";

    paraMap.put("className", className);
    paraMap.put("module", module);
    paraMap.put("moduleName", moduleName);
    paraMap.put("moduleNameSmall", moduleNameSmall);


    paraMap.put("columnList", columnList);
    paraMap.put("tableComment", tableComment);
    paraMap.put("date", new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date()));
    String filePath = handleWebFilePath(module, moduleName, packages, "", "Form");
    createWebFileByTemplete("module/form/Form.ftl", paraMap, filePath, true);
  }

  public void webCompsPageIndex(String module, String prefix, String tableName, String tableComment, List<TableColumnDTO> columnList) {
    Map<String, Object> paraMap = new HashMap<>();
    String className = changeClassName(tableName, true);
    String moduleName = changeClassName(tableName.replace(prefix + "_", ""), true);

    String moduleNameSmall = changeClassName(tableName.replace(prefix + "_", ""), false);

    String packages =  "components." + moduleNameSmall + ".page";

    paraMap.put("className", className);
    paraMap.put("module", module);
    paraMap.put("moduleName", moduleName);
    paraMap.put("moduleNameSmall", moduleNameSmall);

    paraMap.put("columnList", columnList);
    paraMap.put("tableComment", tableComment);
    paraMap.put("date", new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date()));
    String filePath = handleWebFilePath(module, "", packages, "", "index");
    createWebFileByTemplete("module/page/index.ftl", paraMap, filePath, true);
  }


  /**
   * 根据具体模板生成文件
   *
   * @param templateFileName
   * @param paraMap
   * @param filePath
   */
  public void createFileByTemplete(String templateFileName, Map<String, Object> paraMap, String filePath, boolean isCover) {
    try {
      Configuration cfg = new Configuration(Configuration.VERSION_2_3_28);

      File fileDir = new File(System.getProperty("user.dir") + javaScriptDir);
      System.out.print(fileDir.exists());
      cfg.setDirectoryForTemplateLoading(fileDir);
      //获取模板（template）
      Template template = cfg.getTemplate(templateFileName);
      File file = new File(filePath);
      if (!isCover && file.exists()) {
        log.info("文件已经存在，不能覆盖");
        return;
      }
      File path = new File(file.getParent());
      if (!path.exists()) {
        path.mkdirs();
      }
      Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
      //数据与模板合并（数据+模板=输出）
      template.process(paraMap, out);
      out.flush();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  public void createWebFileByTemplete(String templateFileName, Map<String, Object> paraMap, String filePath, boolean isCover) {
    try {
      Configuration cfg = new Configuration(Configuration.VERSION_2_3_28);

      File fileDir = new File(System.getProperty("user.dir") + webScriptDir);
      System.out.print("---->" + fileDir.exists());
      cfg.setDirectoryForTemplateLoading(fileDir);
      //获取模板（template）
      Template template = cfg.getTemplate(templateFileName);
      File file = new File(filePath);
      if (!isCover && file.exists()) {
        log.info("文件已经存在，不能覆盖");
        return;
      }
      File path = new File(file.getParent());
      if (!path.exists()) {
        path.mkdirs();
      }
      Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
      //数据与模板合并（数据+模板=输出）
      template.process(paraMap, out);
      out.flush();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }


  public static String changeClassName(String str, boolean flag) {

    char[] str1 = new char[]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
        'w', 'x', 'y', 'z'};
    char[] str2 = new char[]{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
        'W', 'X', 'Y', 'Z'};
    String newStr = "";
    int index = 0;

    for (int i = 0; i < str.length(); i++) {
      char ch = str.charAt(i);
      if (flag && i == 0) {
        for (int j = 0; j < str1.length; j++) {
          if (ch == str1[j]) {
            newStr += str2[j];
          }
        }
      } else {
        if (ch == '_') {
          index = i;
        } else {
          if (index != 0 && index + 1 == i) {
            for (int j = 0; j < str1.length; j++) {
              if (ch == str1[j]) {
                newStr += str2[j];
              }
            }
          } else {
            newStr += ch;
          }
        }
      }
    }
    return newStr;

  }

  public List<Map<String, Object>> getTables(String tablePrefix) {
    List<Map<String, Object>> list = dslContext.resultQuery("SELECT table_name,table_comment FROM information_schema.tables where table_schema=? and table_name like ?", JooqFactory.DATABASE, tablePrefix + "%").fetchMaps();
    return list;
  }

  public List<TableColumnDTO> getTableColumnsForDTO(String tableName, String tableComment) {
    List<Column> list = dslContext.resultQuery("select * from information_schema.COLUMNS where table_name=? and table_schema =?  and column_name not in (?)",
        tableName, JooqFactory.DATABASE, entityCommonColums[4]).fetchInto(Column.class);
    return handleList(list, tableName, tableComment);
  }

  public List<TableColumnDTO> getTableColumnsForVO(String tableName, String tableComment) {
    List<Column> list = dslContext.resultQuery("select * from information_schema.COLUMNS where table_name=? and table_schema =?  and column_name not in (?,?,?,?,?)",
        tableName, JooqFactory.DATABASE, entityCommonColums[0], entityCommonColums[1], entityCommonColums[2], entityCommonColums[3], entityCommonColums[4]).fetchInto(Column.class);

    return handleList(list, tableName, tableComment);
  }

  public List<TableColumnDTO> handleList(List<Column> list, String tableName, String tableComment) {
    List<TableColumnDTO> dtoList = new ArrayList<>();
    Map<String, String> columnJavaTypeMap = getJavaType(tableName);
    final TableColumnDTO[] dto = {null};
    list.forEach(column -> {
      dto[0] = new TableColumnDTO();

      String column_name = column.getCOLUMN_NAME();
      String column_type = column.getDATA_TYPE();
      String character_maximum_length = String.valueOf(column.getCHARACTER_MAXIMUM_LENGTH());
      String column_comment = column.getCOLUMN_COMMENT();


      dto[0].setTable_name(tableName);
      dto[0].setTable_comment(tableComment);

      dto[0].setColumn_name(changeClassName(column_name, false));
      dto[0].setColumn_name2(column_name);
      String columnDefault = column.getCOLUMN_DEFAULT();
      if (columnDefault != null && columnDefault.equals("b'0'")) {
        columnDefault = "false";
      } else if (columnDefault != null && columnDefault.equals("b'1'")) {
        columnDefault = "true";
      }
      dto[0].setColumn_default(columnDefault);
      dto[0].setColumn_name3(changeClassName(column_name, true));
      dto[0].setColumn_type(column_type);
      dto[0].setColumn_type_(removeBrackets(column_type).toUpperCase());
      dto[0].setColumn_length(character_maximum_length);
      dto[0].setColumn_comment(column_comment);

      String className = columnJavaTypeMap.get(column_name);
      log.info("ddd-->{}", className);
      dto[0].setColumn_className(className);
      dtoList.add(dto[0]);
    });
    return dtoList;
  }

  public Map<String, String> getJavaType(String tableName) {
    //  获取字段数
    Map<String, String> columnJavaTypeMap = new HashMap<>();
    try {

      Connection conn = dslContext.parsingConnection();

      Statement stmt = conn.createStatement();
      String sql = "select * from " + tableName + " where 1 != 1 ";
      ResultSet rs = stmt.executeQuery(sql);
      ResultSetMetaData rsmd = rs.getMetaData();

      int columns = rsmd.getColumnCount();
      for (int i = 1; i <= columns; i++) {
        //获取字段名
        String columnName = rsmd.getColumnName(i).toLowerCase();
        String columnClassName = rsmd.getColumnClassName(i);

        log.info("getJavaType columnName={}, columnClassName={}", columnName, columnClassName);
        if ("[B".equals(columnClassName)) {
          columnClassName = "byte[]";
        } else if ("java.sql.Timestamp".equals(columnClassName)) {
          columnClassName = "java.util.Date";
        }
        columnJavaTypeMap.put(columnName, columnClassName);
      }
    } catch (Exception e) {
      e.printStackTrace();
    }

    return columnJavaTypeMap;
  }

  public static String removeBrackets(String value) {
    if (value.indexOf("(") != -1)
      value = value.substring(0, value.lastIndexOf("("));
    return value;
  }


  public static class Column implements Serializable {
    private String COLUMN_NAME;
    private String DATA_TYPE;
    private String CHARACTER_MAXIMUM_LENGTH;
    private String COLUMN_DEFAULT;
    private String COLUMN_COMMENT;

    public String getCOLUMN_NAME() {
      return COLUMN_NAME;
    }

    public void setCOLUMN_NAME(String COLUMN_NAME) {
      this.COLUMN_NAME = COLUMN_NAME;
    }

    public String getCHARACTER_MAXIMUM_LENGTH() {
      return CHARACTER_MAXIMUM_LENGTH;
    }

    public void setCHARACTER_MAXIMUM_LENGTH(String CHARACTER_MAXIMUM_LENGTH) {
      this.CHARACTER_MAXIMUM_LENGTH = CHARACTER_MAXIMUM_LENGTH;
    }

    public String getCOLUMN_COMMENT() {
      return COLUMN_COMMENT;
    }

    public void setCOLUMN_COMMENT(String COLUMN_COMMENT) {
      this.COLUMN_COMMENT = COLUMN_COMMENT;
    }

    public String getDATA_TYPE() {
      return DATA_TYPE;
    }

    public void setDATA_TYPE(String DATA_TYPE) {
      this.DATA_TYPE = DATA_TYPE;
    }

    public String getCOLUMN_DEFAULT() {
      return COLUMN_DEFAULT;
    }

    public void setCOLUMN_DEFAULT(String COLUMN_DEFAULT) {
      this.COLUMN_DEFAULT = COLUMN_DEFAULT;
    }
  }

}